﻿using ArtMath.Component.Line;
using ArtMath.Component.Polygon;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using System;
using System.Linq;
using ArtMath.CommonType;
using ArtMath.Attribute;

namespace ArtMath.Component.Circle
{
    /// <summary>
    /// 内切圆
    /// </summary>
    [Serializable]
    public class InscribedCircle : ConstructedCircleBase
    {
        public InscribedCircle(SketchPad pad, string name, PolygonBase pol, bool addToList = true) : base(pad, name, pol, ToolTypeName.nqy, addToList)
        {

        }
        [MultilingualDisplayName("Polygon")]
        public override LineBase Line { get => base.Line; set => base.Line = value; }
        public override PointD NewCenter
        {
            get
            {
                if (newCenter == null)
                {
                    TriangleBase tb = Line as TriangleBase;
                    if (tb != null)
                    {
                        newCenter = tb.NewInCenter;
                    }
                    else
                    {
                        PolygonBase pol = Line as PolygonBase;
                        if (pol != null)
                        {
                            ObjectState = Utility.GetInscribedCircleCenter(pol.Lines.Select((LineSegmentBase line) => { return line.Point1.NewXY; }).ToArray(), out newCenter) ? ObjectState.Normal : ObjectState.Deleted;
                        }
                    }
                }
                return newCenter.Value;
            }
        }
        public override PointD NewPCenter
        {
            get
            {
                if (newPcenter == null)
                {
                    TriangleBase tb = Line as TriangleBase;
                    if (tb != null)
                    {
                        newPcenter = tb.NewPInCenter;
                    }
                    else
                    {
                        newPcenter = Pad.XYToPxy(NewCenter);
                    }
                }
                return newPcenter.Value;
            }
        }
        public override double NewRadius
        {
            get
            {
                PolygonBase pol = Line as PolygonBase;
                if (pol != null && newRadius == null)
                {
                    newRadius = NewCenter.DistanceTo(pol.Lines[0].NewA, pol.Lines[0].NewB, pol.Lines[0].NewC);
                }
                return newRadius.Value;
            }
        }
        public override void ComponentChanged(ShapeBase component)
        {
            //ske.Center = NewCenter;
            //pske.Center = NewPCenter;
            //Radius = NewRadius;
            newCenter = null;
            newPcenter = null;
            newRadius = null;
            base.ComponentChanged(component);
            ObjectState = ObjectState.Normal;
            PolygonBase pol = Line as PolygonBase;
            if (pol != null && pol.Lines.Count > 3)
            {
                for (int i = 3; i < pol.Lines.Count; i++)
                {
                    if (!Radius.AlmostEqual(Center.DistanceTo(pol.Lines[i].A, pol.Lines[i].B, pol.Lines[i].C)))
                    {
                        ObjectState = ObjectState.Deleted;
                        break;
                    }
                }
            }
            //Deleted = !valid;
        }
    }
}
